package com.anjoyo.xgh.musicshare.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.ShareBean;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.JsonUtil;
import com.anjoyo.xgh.musicshare.util.MD5Util;
import com.anjoyo.xgh.musicshare.util.MusicSpf;
import com.anjoyo.xgh.musicshare.util.StringUtil;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class ShareApi {
	Handler handler;
	String json = "";

	public ShareApi(Handler handler) {
		this.handler = handler;
	}

	public void sharetxtonly(long userid, String msg, String address,
			double lat, double lon) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("user_id", userid + "");
		ajaxParams.put("share_msg", msg);
		ajaxParams.put("share_lat", lat + "");
		ajaxParams.put("share_lon", lon + "");
		ajaxParams.put("address", address);
		finalHttp.get(Constant.SHARE_URL, ajaxParams,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
						try {
							Message message = new Message();
							message.what = Constant.MSG_SHARE_OK;
							JSONObject jsonObject = new JSONObject(t.toString());
							if (jsonObject.getString("code").equals("200")) {
								message.arg1 = 200;
							} else {
								message.arg1 = 404;
							}
							message.obj = jsonObject;
							handler.sendMessage(message);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	public void share(long userid, String msg, File imgFile, String address,
			int musicid, double lat, double lon, File audioFile, int time) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("user_id", userid + "");
		ajaxParams.put("music_id", musicid + "");
		ajaxParams.put("share_lat", lat + "");
		ajaxParams.put("share_lon", lon + "");
		try {
			ajaxParams.put("share_msg", URLEncoder.encode(msg, "UTF-8"));
			ajaxParams.put("address", URLEncoder.encode(address, "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {
			if (imgFile != null && imgFile.exists()) {
				System.out.println("分享图片");
				ajaxParams.put("share_img_lager", imgFile);
			}
			if (time >= 3) {
				if (audioFile != null && audioFile.exists()) {
					System.out.println("分享录音");
					ajaxParams.put("share_file", audioFile);
					ajaxParams.put("share_file_time", time + "");
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		finalHttp.configCharset("GBK");
		finalHttp.post(Constant.SHARE_URL, ajaxParams,
				new AjaxCallBack<Object>() {
					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
						try {
							Message message = new Message();
							message.what = Constant.MSG_SHARE_OK;
							JSONObject jsonObject = new JSONObject(t.toString());
							if (jsonObject.getString("code").equals("200")) {
								message.arg1 = 200;
							} else {
								message.arg1 = 404;
							}
							message.obj = jsonObject;
							handler.sendMessage(message);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	public void getShareList(long userid, String username,
			final MusicDBhelper dBhelper) {

		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("userid", userid + "");
		params.put("username", username);
		finalHttp.get(Constant.SHARE_LIST, params, new AjaxCallBack<Object>() {
			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				if (t != null) {
					dBhelper.deletShareList();
					dBhelper.saveshare(t.toString());
					System.out.println("加入到数据库");
					System.out.println(t.toString());
					Message message = new Message();
					message.what = Constant.MSG_SHARELIST_OK;
					message.obj = JsonUtil.parseShareList(t.toString());
					handler.sendMessage(message);
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				if (strMsg != null) {
					System.out.println("失败" + t.toString());
				}
			}
		});
	}

	public void likeShare(long shareid, final Context con) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		System.out.println("用户ID：" + MusicSpf.getloginUser(con).getId() + "");
		params.put("userid", MusicSpf.getloginUser(con).getId() + "");
		params.put("username", MusicSpf.getloginUser(con).getName());
		params.put("shareid", shareid + "");
		finalHttp.post(Constant.SHARE_LIKE, params, new AjaxCallBack<Object>() {
			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Toast.makeText(con, t.toString(), 0).show();
			}
		});
	}
}
